﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using SpreadsheetGear.Windows.Forms;
using WorkbookDesigner.Addons.SpreadsheetEditor.Presenter;

//using SpreadsheetGear.Windows.Forms;

namespace WorkbookDesigner.Addons.SpreadsheetEditor.UI
{
    public partial class EditorForm : Form, ISpreadEditorView
    {
        public EditorForm()
        {
            InitializeComponent();
            Init();
        }


        public ISpreadEditorPresenter Presenter { get; private set; }

        IMissionView ISpreadEditorView.MissionView => missionView1;
        // ReSharper disable once ConvertToAutoProperty
        public TreeView FileTreeView => treeViewFileList;
        BackgroundWorker ISpreadEditorView.BackgroundWorker => backgroundWorker;
        public IBookView BookView => wfBookView1;
        public ProgressBar ProgressBar => progressBar;

        public void Init()
        {
            Presenter = new MainPresenter(this);
        }

        void ISpreadEditorView.DisableView()
        {
            //buttonAddFiles.Enabled =
            //buttonAddFolder.Enabled =
            buttonCancelSelectAll.Enabled =
            buttonRemove.Enabled =
            buttonSelectAll.Enabled =
            buttonStart.Enabled = false;
        }

        void ISpreadEditorView.EnableView()
        {
            //buttonAddFiles.Enabled =
            //buttonAddFolder.Enabled =
            buttonCancelSelectAll.Enabled =
            buttonRemove.Enabled =
            buttonSelectAll.Enabled =
            buttonStart.Enabled = true;

        }

        void ISpreadEditorView.BeginInvoke(Delegate method, params object[] args)
        {
            this.BeginInvoke(method, args);
        }
        private void EditorForm_Load(object sender, EventArgs e)
        {
            treeViewFileList.Nodes.Clear();

            textBoxTemplateDirectory.DataBindings.Clear();
            //string dataMember = nameof(Presenter.TemplateModel.TemplateDirectory);

            textBoxTemplateDirectory.DataBindings.Add(new Binding("Text", Presenter.TemplateModel, "Directory", false, DataSourceUpdateMode.OnPropertyChanged));

            Presenter.OnBtn_SelectTemplateDir_Click();
        }

        private void treeViewFileList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Presenter.OnTreeView_NodeMouseClick(e);
        }

        private void treeViewFileList_AfterCheck(object sender, TreeViewEventArgs e)
        {
            Presenter.OnTreeView_AfterCheck(e);
        }

        private void buttonSelectDirectory_Click(object sender, EventArgs e)
        {
            Presenter.OnBtn_SelectTemplateDir_Click();

        }

        private void EditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            textBoxTemplateDirectory.DataBindings.Clear();
            Presenter.OnViewClosing(e);
            Presenter = null;
        }

        private void buttonAddFiles_Click(object sender, EventArgs e)
        {
            Presenter.OnBtnAddFiles_Click();
        }

        private void buttonAddFolder_Click(object sender, EventArgs e)
        {
            Presenter.OnAddDirectory();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            Presenter.OnBtnRemove_Click();
        }

        private void buttonSelectAll_Click(object sender, EventArgs e)
        {
            Presenter.OnBtnSelectAll_Click();
        }

        private void buttonCancelSelectAll_Click(object sender, EventArgs e)
        {
            treeViewFileList.Nodes.Clear();//todo 关闭所有工作簿
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Presenter.ExecuteAll();
        }

        private void EditorForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void buttonSaveAll_Click(object sender, EventArgs e)
        {
            Presenter.OnBtnSaveAll_Click();
        }
    }
}
